package com.sherlock.demo.interceptor;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handler1 = (HandlerMethod) handler;

            Method method = handler1.getMethod();
            String methodName = method.getName();
            // 只放行 test方法
            System.out.println(methodName);
            if (! "test".equalsIgnoreCase(methodName)) {
                throw new Exception("111");
            }
        } else {
            throw new Exception("111");
        }

        return true;
    }
}
